//
//  HMNoteDetailsController.m
//  HeartMark
//
//  Created by rimi on 2016/11/10.
//  Copyright © 2016年 黄华强. All rights reserved.
//

#import "HMNoteDetailsController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "HMAlertView.h"

@interface HMNoteDetailsController ()
//笔记视图属性
@property (nonatomic,strong) UITextView *noteTextView;
@property (nonatomic,assign) NSInteger blockNum;

@end

@implementation HMNoteDetailsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _blockNum = 0;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
   
    [self setupUI];
}


- (void)setupUI {
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    _noteTextView = [[UITextView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 0.03, 80, SCREEN_WIDTH * 0.94, SCREEN_HEIGHT - 80)];
    _noteTextView.showsVerticalScrollIndicator = NO;
    [_noteTextView setEditable:NO];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8; //行距
    paragraphStyle.firstLineHeadIndent = 0;
    
    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:TEXT_FONT], NSParagraphStyleAttributeName:paragraphStyle};
    
    _noteTextView.attributedText = [[NSAttributedString alloc]initWithString: _model.noteMessage attributes:attributes];

    
    [self.view addSubview:_noteTextView];
    
    
    UIBarButtonItem *likeBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"已收藏_32"] style:UIBarButtonItemStylePlain target:self action:@selector(collectionBtnClicked:)];
    self.navigationItem.rightBarButtonItem = likeBtn;
    
    if (_isLike) {
        [likeBtn setImage:[UIImage imageNamed:@"已收藏_32"] ];
    } else {
        
        [likeBtn setImage:[UIImage imageNamed:@"未收藏_32"] ];
    }

    
}



- (void)sharkBtnClicked:(UIButton *)button {
    
}

- (void)collectionBtnClicked:(UIBarButtonItem *)button {

    [button setEnabled:NO];
    AVUser *currentUser = [AVUser currentUser];
    AVObject *obj = _model.object;
    
    [AVObject saveAllInBackground:@[obj] block:^(BOOL succeeded, NSError *error) {
        if (error) {
            [HMAlertView bottomAlertView:@"网络异常"];
        } else {
            // 保存成功
            AVRelation *relation = [currentUser relationForKey:@"collectionNote"];
            
            if (_isLike) {
                [relation removeObject:obj];
                NSDictionary *userInfo = @{@"isAdd":@"NO"};
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"collectionNotification" object:obj userInfo:userInfo];
                self.myBlock(obj,@"NO");
                
            } else {
                [relation addObject:obj];
                NSDictionary *userInfo = @{@"isAdd":@"YES"};
               
                [[NSNotificationCenter defaultCenter] postNotificationName:@"collectionNotification" object:obj userInfo:userInfo];
                self.myBlock(obj,@"YES");
            }
            
            [currentUser saveInBackground];
            _blockNum += 1;
            if (_blockNum == 2) {
                _blockNum = 0;
                _isLike = !_isLike;
                
                if (_isLike) {
                    [button setImage:[UIImage imageNamed:@"已收藏_32"] ];
                    [HMAlertView bottomAlertView:@"收藏成功！"];
                    
                } else {
                    
                    [button setImage:[UIImage imageNamed:@"未收藏_32"]];
                    [HMAlertView bottomAlertView:@"取消收藏成功！"];
                }
                [button setEnabled:YES];
            }
            
        }
    }];
    
    [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
        if (error) {
            [HMAlertView bottomAlertView:@"网络异常"];
        } else {
        
        if (_isLike) {
            [obj incrementKey:@"collectNumber" byAmount:@(-1)];
        } else {
            // 原子增加查看的次数
            [obj incrementKey:@"collectNumber"];
        }
        // 保存时自动取回云端最新数据
        obj.fetchWhenSave = true;
        [obj saveInBackground];
        _blockNum += 1;
        if (_blockNum == 2) {
            _blockNum = 0;
            _isLike = !_isLike;
            
            if (_isLike) {
                [button setImage:[UIImage imageNamed:@"已收藏_32"] ];
                [HMAlertView bottomAlertView:@"收藏成功！"];
            } else {
                
                [button setImage:[UIImage imageNamed:@"未收藏_32"] ];
                [HMAlertView bottomAlertView:@"取消收藏成功！"];
            }
            [button setEnabled:YES];
        }
        }
    }];
    
}

//重写系统的一些方法
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.tabBarController.tabBar setHidden:NO];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
